package com.newx.file.config;

import com.newx.file.condition.NewxFileCondition;
import com.newx.file.handler.NewxFileException;
import com.newx.file.service.NewxFileService;
import com.newx.file.service.NewxFileServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author Binary Wang
 */
@Configuration
@Conditional(NewxFileCondition.class)
@EnableConfigurationProperties(NewxFileProperties.class)
@Slf4j
@AllArgsConstructor
public class NewxFileConfiguration {
  private NewxFileProperties properties;

  @Bean
  @ConditionalOnMissingBean
  public NewxFileConfig newxFileConfig() {
    NewxFileConfig config = new NewxFileConfig();
    List<NewxFileProperties.Local> locals = this.properties.getLocal();
    if (locals != null || locals.size() >0 ) {
      config.setLocalFilePath(locals.get(0).getFilePath());
      config.setLocaFileUrlPrefix(locals.get(0).getFileUrlPrefix());
      config.setLocalBucket(locals.get(0).getBucket());
    }
    log.info("本地文件上传相关配置：{}", config);
    List<NewxFileProperties.Aliyun> aliyun = this.properties.getAliyun();
    if (aliyun != null && aliyun.size() > 0) {
      config.setAliyunAccessKeyId(aliyun.get(0).getAccessKeyId());
      config.setAliyunAccessKeySecret(aliyun.get(0).getAccessKeySecret());
      config.setAliyunBucket(aliyun.get(0).getBucket());
      config.setAliyunDomain(aliyun.get(0).getDomain());
      config.setAliyunEndpoint(aliyun.get(0).getEndpoint());
      config.setAliyunPackagePath(aliyun.get(0).getPackagePath());
    }
    log.info("阿里云文件上传相关配置：{}", config);
    List<NewxFileProperties.Tencent> tencents = this.properties.getTencent();
    if (tencents != null && tencents.size() > 0) {
      config.setTencentSecretId(tencents.get(0).getSecretId());
      config.setTencentSecretKey(tencents.get(0).getSecretKey());
      config.setTencentRegion(tencents.get(0).getRegion());
      config.setTencentBucketName(tencents.get(0).getBucketName());
      config.setTencentPackagePath(tencents.get(0).getPackagePath());
    }
    log.info("腾讯云文件上传相关配置：{}", config);
    return config;
  }
}
